/**
 * Created by zhangxiaoyu on 16/12/16.
 */
angular.module("hyt.personCtrl",[])

  .controller('personCtrl', function($scope,$state,$ionicLoading,Storage,$ionicPopup,$ionicHistory,ENV,Version,$timeout,$cordovaFileTransfer,$cordovaFileOpener2,$window) {
    var userMsg=Storage.get('loginResult');
    var loginMsg=Storage.get('user');
    var download_url;
    $scope.mobile=userMsg.rows.username;
    var version={
      versionno:ENV.version,
      app_type:"1",
      userMsg:{
        username:loginMsg.username,
        password:loginMsg.password,
        uuid:loginMsg.uuid
      }
    };
    Version.getVersion(JSON.stringify(version));
    $scope.$on('Version.Updated', function() {
      var result=Version.getResult().rows;
      if (result==undefined){
        $scope.cn=0;
      }else {
        $scope.cn=1;
        download_url=result.DOWNLOAD_URL;
      }
    });
    //检查更新
    $scope.checkAPK=function(){
      if ($scope.cn==0){
        $ionicLoading.show({
          noBackdrop: true,
          template: "当前已是最新版本",
          duration: 1500
        });
      }else if ($scope.cn==1){
        showUpdateConfirm();
      }
    };
    function showUpdateConfirm() {
      var confirmPopup = $ionicPopup.confirm({
        title: '版本升级',
        cancelText: '取消',
        okText: '升级'
      });

      confirmPopup.then(function (res) {
        if (res) {
            $ionicLoading.show({
              template: "已经下载：0%"
            });
            var url = download_url; //可以从服务端获取更新APP的路径
            var targetPath= "file:///storage/sdcard0/hytAndroid/hyt.apk";
            //APP下载存放的路径，可以使用cordova file插件进行相关配置
            var trustHosts = true;
            var options = {};

            $cordovaFileTransfer.download(url,targetPath,options,trustHosts).then(function (result) {
              // 打开下载下来的APP
              $cordovaFileOpener2.open(targetPath, 'application/vnd.android.package-archive'
              ).then(function () {
                // 成功
              }, function (err) {
                // 错误
              });
              $ionicLoading.hide();
            }, function (err) {
              $ionicLoading.show({template: '下载失败！', noBackdrop: true, duration: 2000});
            }, function (progress) {
              //进度，这里使用文字显示下载百分比
              $timeout(function () {
                var downloadProgress = (progress.loaded / progress.total) * 100;
                $ionicLoading.show({
                  template: "已经下载：" + Math.floor(downloadProgress) + "%"
                });
                if (downloadProgress > 99) {
                  $ionicLoading.hide();
                }
              })
            });
        } else {
          // 取消更新
        }
      });
    }
    $scope.callPhone = function (mobilePhone) {
      var confirmPopup = $ionicPopup.confirm({
        title:mobilePhone,
        okText:'呼叫',
        cancelText:'取消'
      });
      confirmPopup.then(function(res) {
        if (res) {
          $window.location.href = "tel:" + mobilePhone;
        }else {

        }
      });
    };
    //退出登录
    $scope.exitLogin=function(){
      var confirmPopup = $ionicPopup.confirm({
        title:'退出当前账号',
        template: '您确定要退出吗？',
        okText:'确定',
        cancelText:'取消'
      });
      confirmPopup.then(function(res) {
        if(res){
          Storage.set('isLogin',0);
          Storage.remove('loginResult');
          $ionicHistory.clearCache();
          $ionicHistory.clearHistory();
          $state.go('login');
        }
        else {
          return false;
        }
      });
    };
  })
  .controller('ReceiveAddressCtrl', function($scope,$state,$ionicLoading,Storage,addressList,$ionicPopup,DeleteAddress,$stateParams) {
    var loginMsg=Storage.get('user');
    var userMsg=Storage.get('loginResult');
    $scope.showloading=true;
    var addressInfo={
      addressId:userMsg.rows.id,
      flag:'1', //0为发货人，1为收货人
      userMsg:{
        username:loginMsg.username,
        password:loginMsg.password,
        uuid:loginMsg.uuid
      }
    };
    addressList.getAddressList(JSON.stringify(addressInfo));
    //下拉刷新
    $scope.doRefresh=function(){
      addressList.getAddressList(JSON.stringify(addressInfo));
      $scope.$broadcast('scroll.refreshComplete');
    };
    $scope.$on('AddressList.Updated', function() {
      $scope.addressData=addressList.getResult().rows;
      $scope.showloading=false;
    });
    $scope.chooseAddress=function(data){
      if ($stateParams.isChoose==1&&$stateParams.htmlId==0){
        $state.go('addOrder');
        Storage.set('receiveAddress',JSON.stringify(data));
      }else if($stateParams.isChoose==1&&$stateParams.htmlId==1){
        $state.go('changeOrder',{id:Storage.get('changeOrder').orderId});
        Storage.set('receiveAddress',JSON.stringify(data));
      }
    };
    $scope.addAddress=function(){
      $state.go('addReceiveAddress',{isChoose:$stateParams.isChoose,htmlId:$stateParams.htmlId});
    };
    $scope.changeAddress=function(id){
      $state.go('changeReceiveAddress',{id:id,isChoose:$stateParams.isChoose,htmlId:$stateParams.htmlId});
    };
    $scope.deleteAddress=function(id){
      var deleteAddress={
        id:id,
        userMsg:{
          username:loginMsg.username,
          password:loginMsg.password,
          uuid:loginMsg.uuid
        }
      };
      var receipt = $ionicPopup.confirm({
        title : '是否删除地址',
        cancelText : '取消',
        cancelType : 'button-stable',
        okText : '删除'
      });
      receipt.then(function(r){
        if(r){
          DeleteAddress.getDeleteAddress(JSON.stringify(deleteAddress));
          $scope.$on('DeleteAddress.Updated',function(){
            var result=DeleteAddress.getResult();
            if (result.success){
              $ionicLoading.show({
                noBackdrop: true,
                template: "删除成功",
                duration: 1500
              });
              addressList.getAddressList(JSON.stringify(addressInfo));
            }else {
              $ionicLoading.show({
                noBackdrop: true,
                template: "删除失败",
                duration: 1500
              });
            }
          });
        }else{
          console.log('取消');
        }
      });
    }
    $scope.myGoBack=function(){
      if ($stateParams.isChoose==1&&$stateParams.htmlId==0){
        $state.go('addOrder');
      }else if($stateParams.isChoose==1&&$stateParams.htmlId==1){
        $state.go('changeOrder',{id:Storage.get('changeOrder').orderId});
      }else {
        $state.go('tab.person');
      }
    }
  })
  .controller('AddReceiveCtrl', function($scope,$state,$ionicLoading,Storage,District,AddAddress,$stateParams) {
    var loginMsg=Storage.get('user');
    var userMsg=Storage.get('loginResult');
    //省市区
    var province={
      type:"province",
      userMsg:{
        username:loginMsg.username,
        password:loginMsg.password,
        uuid:loginMsg.uuid
      }
    };
    var district={
      type:"",
      parentId:'',
      userMsg:{
        username:loginMsg.username,
        password:loginMsg.password,
        uuid:loginMsg.uuid
      }
    };
    District.getProvince(JSON.stringify(province));
    $("#province").change(function(e) {
      if ($('#province').val()=='地区选择'){
        $scope.cityData=null;
        $scope.areaData=null;
      }else {
        district.parentId=$('#province').val();
        District.getCity(JSON.stringify(district));
      }
    });
    $("#city").change(function(e) {
      district.parentId=$('#city').val();
      District.getArea(JSON.stringify(district));
    });
    $scope.$on('Province.Updated', function() {
      $scope.provinceData=District.getPro().rows;
    });
    $scope.$on('City.Updated', function() {
      $scope.cityData=District.getCit().rows;
      district.parentId=$scope.cityData[0].id;
      District.getArea(JSON.stringify(district));
    });
    $scope.$on('Area.Updated', function() {
      $scope.areaData=District.getAre().rows;
    });
    $scope.addReceiveAddress={
      addressInfo:{
        customerId:userMsg.rows.id,
        customerPhone:'',
        customerName:'',
        consigneeProvince:'',
        consigneeCity:'',
        consigneeDistrict:'',
        customerAddress:'',
        isDefault:'',
        defaultAddress:'',//是否为默认地址  1为默认，0不默认
        customerflag:'1'
      },
      userMsg:{
        username:loginMsg.username,
        password:loginMsg.password,
        uuid:loginMsg.uuid
      }
    };
    $scope.saveReceiveAddress=function(){
      if ($scope.addReceiveAddress.addressInfo.customerPhone==''){
        $ionicLoading.show({
          noBackdrop: true,
          template: '请填写收货电话',
          duration: 1500
        });
      }else if($scope.addReceiveAddress.addressInfo.customerName==''){
        $ionicLoading.show({
          noBackdrop: true,
          template: '请填写收货人姓名',
          duration: 1500
        });
      }else if($("#province option:selected").text()=='地区选择'){
        $ionicLoading.show({
          noBackdrop: true,
          template: '请选择收货地区',
          duration: 1500
        });
      }else if($scope.addReceiveAddress.addressInfo.customerAddress==''){
        $ionicLoading.show({
          noBackdrop: true,
          template: '请填写详细收货地址',
          duration: 1500
        });
      }else if(!(/^1[34578]\d{9}$/.test($scope.addReceiveAddress.addressInfo.customerPhone))){
        $ionicLoading.show({
          noBackdrop: true,
          template: "收货电话格式有误",
          duration: 1500
        });
      }else {
        $scope.addReceiveAddress.addressInfo.consigneeProvince=$("#province").val();
        $scope.addReceiveAddress.addressInfo.consigneeCity=$("#city").val();
        $scope.addReceiveAddress.addressInfo.consigneeDistrict=$("#area").val();
        if ($scope.addReceiveAddress.addressInfo.isDefault){
          $scope.addReceiveAddress.addressInfo.defaultAddress='1';
        }else {
          $scope.addReceiveAddress.addressInfo.defaultAddress='0';
        }
        AddAddress.getAddAddress(JSON.stringify($scope.addReceiveAddress));
      }
    };
    $scope.$on('AddAddress.Updated', function() {
      var result=AddAddress.getResult();
      if (result.success){
        $ionicLoading.show({
          noBackdrop: true,
          template: '添加成功',
          duration: 1500
        });
        $state.go('receiveAddress',{isChoose:$stateParams.isChoose,htmlId:$stateParams.htmlId});
      }
    });
    $scope.goBackList=function(){
      $state.go('receiveAddress',{isChoose:$stateParams.isChoose,htmlId:$stateParams.htmlId});
    }
  })
  .controller('ChangeReceiveCtrl', function($scope,$state,$ionicLoading,Storage,$stateParams,District,AddAddress,AddressDetail,$timeout) {
    var loginMsg=Storage.get('user');
    var userMsg=Storage.get('loginResult');
    //省市区
    var provinceId,cityId,areaId;
    var isOnce=true;
    var province={
      type:"province",
      userMsg:{
        username:loginMsg.username,
        password:loginMsg.password,
        uuid:loginMsg.uuid
      }
    };
    var district={
      type:"",
      parentId:'',
      userMsg:{
        username:loginMsg.username,
        password:loginMsg.password,
        uuid:loginMsg.uuid
      }
    };
    var addressDetail={
      id:$stateParams.id,
      userMsg:{
        username:loginMsg.username,
        password:loginMsg.password,
        uuid:loginMsg.uuid
      }
    };
    $scope.defaultAddress={
      isDefault:''
    };
    AddressDetail.getAddressDetail(JSON.stringify(addressDetail));
    $scope.$on('AddressDetail.Updated', function() {
      $scope.addressDetailData=AddressDetail.getResult().rows;
      provinceId=$scope.addressDetailData.consigneeProvince;
      cityId=$scope.addressDetailData.consigneeCity;
      areaId=$scope.addressDetailData.consigneeDistrict;
      if ($scope.addressDetailData.defaultAddress=='1'){
        $scope.defaultAddress.isDefault=true;
      }else {
        $scope.defaultAddress.isDefault=false;
      }
      District.getProvince(JSON.stringify(province));
    });
    $("#province").change(function(e) {
      isOnce=false;
      if ($('#province').val()=='地区选择'){
        $scope.cityData=null;
        $scope.areaData=null;
      }else {
        district.parentId=$('#province').val();
        District.getCity(JSON.stringify(district));
      }
    });
    $("#city").change(function(e) {
      isOnce=false;
      district.parentId=$('#city').val();
      District.getArea(JSON.stringify(district));
    });
    $scope.$on('Province.Updated', function() {
      $scope.provinceData=District.getPro().rows;
      if (isOnce==true){
        $timeout(function(){
          $('#province').val(provinceId);
          district.parentId=provinceId;
          District.getCity(JSON.stringify(district));
        },100);
      }
    });
    $scope.$on('City.Updated', function() {
      $scope.cityData=District.getCit().rows;
      if (isOnce==false){
        district.parentId=$scope.cityData[0].id;
        District.getArea(JSON.stringify(district));
      }else {
        $timeout(function(){
          $('#city').val(cityId);
          district.parentId=cityId;
          District.getArea(JSON.stringify(district));
        },100);
      }
    });
    $scope.$on('Area.Updated', function() {
      $scope.areaData=District.getAre().rows;
      if (isOnce==true){
        $timeout(function(){
          $('#area').val(areaId);
        },100);
      }
    });
    $scope.saveReceiveAddress=function(){
      var changeReceiveAddress={
        addressInfo:{
          id:$stateParams.id,
          customerId:userMsg.rows.id,
          customerPhone:$scope.addressDetailData.customerPhone,
          customerName:$scope.addressDetailData.customerName,
          consigneeProvince:$("#province").val(),
          consigneeCity:$("#city").val(),
          consigneeDistrict:$("#area").val(),
          customerAddress:$scope.addressDetailData.customerAddress,
          defaultAddress:'',//是否为默认地址  1为默认，0不默认
          customerflag:'1'
        },
        userMsg:{
          username:loginMsg.username,
          password:loginMsg.password,
          uuid:loginMsg.uuid
        }
      };
      if (changeReceiveAddress.addressInfo.customerPhone==''){
        $ionicLoading.show({
          noBackdrop: true,
          template: '请填写收货电话',
          duration: 1500
        });
      }else if(changeReceiveAddress.addressInfo.customerName==''){
        $ionicLoading.show({
          noBackdrop: true,
          template: '请填写收货人姓名',
          duration: 1500
        });
      }else if($("#province option:selected").text()=='地区选择'){
        $ionicLoading.show({
          noBackdrop: true,
          template: '请选择收货地区',
          duration: 1500
        });
      }else if(changeReceiveAddress.addressInfo.customerAddress==''){
        $ionicLoading.show({
          noBackdrop: true,
          template: '请填写详细收货地址',
          duration: 1500
        });
      }else if(!(/^1[34578]\d{9}$/.test(changeReceiveAddress.addressInfo.customerPhone))){
        $ionicLoading.show({
          noBackdrop: true,
          template: "收货电话格式有误",
          duration: 1500
        });
      }else {
        if ($scope.defaultAddress.isDefault){
          changeReceiveAddress.addressInfo.defaultAddress='1';
        }else {
          changeReceiveAddress.addressInfo.defaultAddress='0';
        }
        AddAddress.getAddAddress(JSON.stringify(changeReceiveAddress));
      }
    };
    $scope.$on('AddAddress.Updated', function() {
      var result=AddAddress.getResult();
      if (result.success){
        $ionicLoading.show({
          noBackdrop: true,
          template: '修改成功',
          duration: 1500
        });
        $state.go('receiveAddress',{isChoose:$stateParams.isChoose,htmlId:$stateParams.htmlId});
      }
    });
    $scope.goBackList=function(){
      $state.go('receiveAddress',{isChoose:$stateParams.isChoose,htmlId:$stateParams.htmlId});
    }
  })
  .controller('ShippingAddressCtrl', function($scope,$state,$ionicLoading,Storage,addressList,$ionicPopup,DeleteAddress,$stateParams) {
    var loginMsg=Storage.get('user');
    var userMsg=Storage.get('loginResult');
    $scope.showloading=true;
    var addressInfo={
      addressId:userMsg.rows.id,
      flag:'0', //0为发货人，1为收货人
      userMsg:{
        username:loginMsg.username,
        password:loginMsg.password,
        uuid:loginMsg.uuid
      }
    };
    addressList.getAddressList(JSON.stringify(addressInfo));
    //下拉刷新
    $scope.doRefresh=function(){
      addressList.getAddressList(JSON.stringify(addressInfo));
      $scope.$broadcast('scroll.refreshComplete');
    };
    $scope.$on('AddressList.Updated', function() {
      $scope.addressData=addressList.getResult().rows;
      $scope.showloading=false;
    });
    $scope.chooseAddress=function(data){
      if ($stateParams.isChoose==1&&$stateParams.htmlId==0){
        $state.go('addOrder');
        Storage.set('shippingAddress',JSON.stringify(data));
      }else if($stateParams.isChoose==1&&$stateParams.htmlId==1){
        $state.go('changeOrder',{id:Storage.get('changeOrder').orderId});
        Storage.set('shippingAddress',JSON.stringify(data));
      }
    };
    $scope.addAddress=function(){
      $state.go('addShipperAddress',{isChoose:$stateParams.isChoose,htmlId:$stateParams.htmlId});
    };
    $scope.changeAddress=function(id){
      $state.go('changeShipperAddress',{id:id,isChoose:$stateParams.isChoose,htmlId:$stateParams.htmlId});
    };
    $scope.deleteAddress=function(id){
      var deleteAddress={
        id:id,
        userMsg:{
          username:loginMsg.username,
          password:loginMsg.password,
          uuid:loginMsg.uuid
        }
      };
      var receipt = $ionicPopup.confirm({
        title : '是否删除地址',
        cancelText : '取消',
        cancelType : 'button-stable',
        okText : '删除'
      });
      receipt.then(function(r){
        if(r){
          DeleteAddress.getDeleteAddress(JSON.stringify(deleteAddress));
          $scope.$on('DeleteAddress.Updated',function(){
            var result=DeleteAddress.getResult();
            if (result.success){
              $ionicLoading.show({
                noBackdrop: true,
                template: "删除成功",
                duration: 1500
              });
              addressList.getAddressList(JSON.stringify(addressInfo));
            }else {
              $ionicLoading.show({
                noBackdrop: true,
                template: "删除失败",
                duration: 1500
              });
            }
          });
        }else{
          console.log('取消');
        }
      });
    }
    $scope.myGoBack=function(){
      if ($stateParams.isChoose==1&&$stateParams.htmlId==0){
        $state.go('addOrder');
      }else if($stateParams.isChoose==1&&$stateParams.htmlId==1){
        $state.go('changeOrder',{id:Storage.get('changeOrder').orderId});
      }else {
        $state.go('tab.person');
      }
    }
  })
  .controller('AddShippingCtrl', function($scope,$state,$ionicLoading,Storage,District,AddAddress,$stateParams) {
    var loginMsg=Storage.get('user');
    var userMsg=Storage.get('loginResult');
    //省市区
    var province={
      type:"province",
      userMsg:{
        username:loginMsg.username,
        password:loginMsg.password,
        uuid:loginMsg.uuid
      }
    };
    var district={
      type:"",
      parentId:'',
      userMsg:{
        username:loginMsg.username,
        password:loginMsg.password,
        uuid:loginMsg.uuid
      }
    };
    District.getProvince(JSON.stringify(province));
    $("#province").change(function(e) {
      if ($('#province').val()=='地区选择'){
        $scope.cityData=null;
        $scope.areaData=null;
      }else {
        district.parentId=$('#province').val();
        District.getCity(JSON.stringify(district));
      }
    });
    $("#city").change(function(e) {
      district.parentId=$('#city').val();
      District.getArea(JSON.stringify(district));
    });
    $scope.$on('Province.Updated', function() {
      $scope.provinceData=District.getPro().rows;
    });
    $scope.$on('City.Updated', function() {
      $scope.cityData=District.getCit().rows;
      district.parentId=$scope.cityData[0].id;
      District.getArea(JSON.stringify(district));
    });
    $scope.$on('Area.Updated', function() {
      $scope.areaData=District.getAre().rows;
    });
    $scope.addShippingAddress={
      addressInfo:{
        customerId:userMsg.rows.id,
        customerPhone:'',
        customerName:'',
        consigneeProvince:'',
        consigneeCity:'',
        consigneeDistrict:'',
        customerAddress:'',
        isDefault:'',
        defaultAddress:'',//是否为默认地址  1为默认，0不默认
        customerflag:'0'
      },
      userMsg:{
        username:loginMsg.username,
        password:loginMsg.password,
        uuid:loginMsg.uuid
      }
    };
    $scope.saveShippingAddress=function(){
      if ($scope.addShippingAddress.addressInfo.customerPhone==''){
        $ionicLoading.show({
          noBackdrop: true,
          template: '请填写发货电话',
          duration: 1500
        });
      }else if($scope.addShippingAddress.addressInfo.customerName==''){
        $ionicLoading.show({
          noBackdrop: true,
          template: '请填写发货人姓名',
          duration: 1500
        });
      }else if($("#province option:selected").text()=='地区选择'){
        $ionicLoading.show({
          noBackdrop: true,
          template: '请选择发货地区',
          duration: 1500
        });
      }else if($scope.addShippingAddress.addressInfo.customerAddress==''){
        $ionicLoading.show({
          noBackdrop: true,
          template: '请填写详细发货地址',
          duration: 1500
        });
      }else if(!(/^1[34578]\d{9}$/.test($scope.addShippingAddress.addressInfo.customerPhone))){
        $ionicLoading.show({
          noBackdrop: true,
          template: "发货电话格式有误",
          duration: 1500
        });
      }else {
        $scope.addShippingAddress.addressInfo.consigneeProvince=$("#province").val();
        $scope.addShippingAddress.addressInfo.consigneeCity=$("#city").val();
        $scope.addShippingAddress.addressInfo.consigneeDistrict=$("#area").val();
        if ($scope.addShippingAddress.addressInfo.isDefault){
          $scope.addShippingAddress.addressInfo.defaultAddress='1';
        }else {
          $scope.addShippingAddress.addressInfo.defaultAddress='0';
        }
        AddAddress.getAddAddress(JSON.stringify($scope.addShippingAddress));
      }
    };
    $scope.$on('AddAddress.Updated', function() {
      var result=AddAddress.getResult();
      if (result.success){
        $ionicLoading.show({
          noBackdrop: true,
          template: '添加成功',
          duration: 1500
        });
        $state.go('shipperAddress',{isChoose:$stateParams.isChoose,htmlId:$stateParams.htmlId});
      }
    });
    $scope.goBackList=function(){
      $state.go('shipperAddress',{isChoose:$stateParams.isChoose,htmlId:$stateParams.htmlId});
    }
  })
  .controller('ChangeShippingCtrl', function($scope,$state,$ionicLoading,Storage,$stateParams,District,AddAddress,AddressDetail,$timeout) {
    var loginMsg=Storage.get('user');
    var userMsg=Storage.get('loginResult');
    //省市区
    var provinceId,cityId,areaId;
    var isOnce=true;
    var province={
      type:"province",
      userMsg:{
        username:loginMsg.username,
        password:loginMsg.password,
        uuid:loginMsg.uuid
      }
    };
    var district={
      type:"",
      parentId:'',
      userMsg:{
        username:loginMsg.username,
        password:loginMsg.password,
        uuid:loginMsg.uuid
      }
    };
    var addressDetail={
      id:$stateParams.id,
      userMsg:{
        username:loginMsg.username,
        password:loginMsg.password,
        uuid:loginMsg.uuid
      }
    };
    $scope.defaultAddress={
      isDefault:''
    };
    AddressDetail.getAddressDetail(JSON.stringify(addressDetail));
    $scope.$on('AddressDetail.Updated', function() {
      $scope.addressDetailData=AddressDetail.getResult().rows;
      provinceId=$scope.addressDetailData.consigneeProvince;
      cityId=$scope.addressDetailData.consigneeCity;
      areaId=$scope.addressDetailData.consigneeDistrict;
      if ($scope.addressDetailData.defaultAddress=='1'){
        $scope.defaultAddress.isDefault=true;
      }else {
        $scope.defaultAddress.isDefault=false;
      }
      District.getProvince(JSON.stringify(province));
    });
    $("#province").change(function(e) {
      isOnce=false;
      if ($('#province').val()=='地区选择'){
        $scope.cityData=null;
        $scope.areaData=null;
      }else {
        district.parentId=$('#province').val();
        District.getCity(JSON.stringify(district));
      }
    });
    $("#city").change(function(e) {
      isOnce=false;
      district.parentId=$('#city').val();
      District.getArea(JSON.stringify(district));
    });
    $scope.$on('Province.Updated', function() {
      $scope.provinceData=District.getPro().rows;
      if (isOnce==true){
        $timeout(function(){
          $('#province').val(provinceId);
          district.parentId=provinceId;
          District.getCity(JSON.stringify(district));
        },100);
      }
    });
    $scope.$on('City.Updated', function() {
      $scope.cityData=District.getCit().rows;
      if (isOnce==false){
        district.parentId=$scope.cityData[0].id;
        District.getArea(JSON.stringify(district));
      }else {
        $timeout(function(){
          $('#city').val(cityId);
          district.parentId=cityId;
          District.getArea(JSON.stringify(district));
        },100);
      }
    });
    $scope.$on('Area.Updated', function() {
      $scope.areaData=District.getAre().rows;
      if (isOnce==true){
        $timeout(function(){
          $('#area').val(areaId);
        },100);
      }
    });
    $scope.saveShippingAddress=function(){
      var changeShippingAddress={
        addressInfo:{
          id:$stateParams.id,
          customerId:userMsg.rows.id,
          customerPhone:$scope.addressDetailData.customerPhone,
          customerName:$scope.addressDetailData.customerName,
          consigneeProvince:$("#province").val(),
          consigneeCity:$("#city").val(),
          consigneeDistrict:$("#area").val(),
          customerAddress:$scope.addressDetailData.customerAddress,
          defaultAddress:'',//是否为默认地址  1为默认，0不默认
          customerflag:'0'
        },
        userMsg:{
          username:loginMsg.username,
          password:loginMsg.password,
          uuid:loginMsg.uuid
        }
      };
      if (changeShippingAddress.addressInfo.customerPhone==''){
        $ionicLoading.show({
          noBackdrop: true,
          template: '请填写发货电话',
          duration: 1500
        });
      }else if(changeShippingAddress.addressInfo.customerName==''){
        $ionicLoading.show({
          noBackdrop: true,
          template: '请填写发货人姓名',
          duration: 1500
        });
      }else if($("#province option:selected").text()=='地区选择'){
        $ionicLoading.show({
          noBackdrop: true,
          template: '请选择发货地区',
          duration: 1500
        });
      }else if(changeShippingAddress.addressInfo.customerAddress==''){
        $ionicLoading.show({
          noBackdrop: true,
          template: '请填写详细发货地址',
          duration: 1500
        });
      }else if(!(/^1[34578]\d{9}$/.test(changeShippingAddress.addressInfo.customerPhone))){
        $ionicLoading.show({
          noBackdrop: true,
          template: "发货电话格式有误",
          duration: 1500
        });
      }else {
        if ($scope.defaultAddress.isDefault){
          changeShippingAddress.addressInfo.defaultAddress='1';
        }else {
          changeShippingAddress.addressInfo.defaultAddress='0';
        }
        AddAddress.getAddAddress(JSON.stringify(changeShippingAddress));
      }
    };
    $scope.$on('AddAddress.Updated', function() {
      var result=AddAddress.getResult();
      if (result.success){
        $ionicLoading.show({
          noBackdrop: true,
          template: '修改成功',
          duration: 1500
        });
        $state.go('shipperAddress',{isChoose:$stateParams.isChoose,htmlId:$stateParams.htmlId});
      }
    });
    $scope.goBackList=function(){
      $state.go('shipperAddress',{isChoose:$stateParams.isChoose,htmlId:$stateParams.htmlId});
    }
  })
  .controller('confirmPwdCtrl', function($scope,$state,$ionicLoading,Storage,$interval,Login) {
    $("#nextStep").attr("disabled", true);
    var loginMsg=Storage.get('user');
    $scope.confirmPwd={
      username:loginMsg.username,
      password:'',
      uuid:loginMsg.uuid,
      key:''
    };
    $scope.changePwNum=function(){
      if($scope.confirmPwd.password!=''){
        $("#nextStep").attr("disabled", false);
      }
      else {
        $("#nextStep").attr("disabled", true);
      }
    };
    $scope.nextStep=function(){
      Login.getLogin(JSON.stringify($scope.confirmPwd));
      $scope.$on('Login.Updated', function() {
        var result = Login.getResult();
        if (result.success==true){
          $state.go('newPw');
        }else {
          $ionicLoading.show({
            noBackdrop: true,
            template: "密码错误",
            duration: 1500
          });
        }
      });
    };
  })
  .controller('changePwdCtrl', function($scope,$state,$ionicLoading,Storage,ForgetPwd) {
    $("#changePw").attr("disabled", true);
    var loginMsg=Storage.get('user');
    $scope.changePwd={
      username:loginMsg.username,
      password:'',
      toPassword:''
    };
    $scope.changePwNum=function(){
      if($scope.changePwd.password!=''&&$scope.changePwd.toPassword){
        $("#changePw").attr("disabled", false);
      }
      else {
        $("#changePw").attr("disabled", true);
      }
    };
    $scope.changePw=function(){
      if ($scope.changePwd.password==$scope.changePwd.toPassword){
        ForgetPwd.getForgetPwd(JSON.stringify($scope.changePwd));
      } else {
        $ionicLoading.show({
          noBackdrop: true,
          template: "密码不一致",
          duration: 1500
        });
      }
    };
    $scope.$on('ForgetPwd.Updated', function() {
      var result=ForgetPwd.getResult();
      if (result.success==true){
        $ionicLoading.show({
          noBackdrop: true,
          template: result.msg,
          duration: 1500
        });
        Storage.set('isLogin',0);
        Storage.remove('loginResult');
        $state.go('login');
      }
    });
  })
  .controller('feedbackCtrl', function($scope,$state,$ionicLoading,Storage,$ionicPopup,$ionicHistory) {
    var userMsg=Storage.get('loginResult');

    $scope.submitOpinion=function(){
      $ionicLoading.show({
        noBackdrop: true,
        template: "提交成功",
        duration: 1500
      });
      $state.go('tab.person');
    };
    //退出登录
    $scope.exitLogin=function(){
      var confirmPopup = $ionicPopup.confirm({
        title:'退出当前账号',
        template: '您确定要退出吗？',
        okText:'确定',
        cancelText:'取消'
      });
      confirmPopup.then(function(res) {
        if(res){
          Storage.set('isLogin',0);
          Storage.remove('loginResult');
          $ionicHistory.clearCache();
          $ionicHistory.clearHistory();
          $state.go('login');
        }
        else {
          return false;
        }
      });
    };
  })
